import torch

# 想看变量是什么对象 → type(x)
# 想看数组或张量中元素类型 → .dtype

input = [3, 4, 6, 5, 7,
         2, 4, 6, 8, 2,
         1, 6, 7, 8, 4,
         9, 7, 4, 6, 2,
         3, 7, 5, 4, 1]
print(type(input))
print(type(input[0]))

input = torch.Tensor(input).view(1, 1, 5, 5)

conv_layer = torch.nn.Conv2d(1, 1, kernel_size=3, padding=1, bias=False)

# | `conv_layer.weight`      | `torch.nn.Parameter`
# | `conv_layer.weight.data` | `torch.Tensor`
# | `kernel`                 | `torch.Tensor`
kernel = torch.Tensor([1, 2, 3, 4, 5, 6, 7, 8, 9]).view(1, 1, 3, 3)
print(kernel.shape)

# Parameter.data 返回的是 Parameter 内部真正存储数值的 Tensor 对象，即去掉了“可追踪梯度”的外壳，变成了一个纯粹的数据张量。
# conv_layer.weight.data = kernel
with torch.no_grad():
    conv_layer.weight.copy_(kernel)

output = conv_layer(input)
print(output)

a1 = torch.Tensor([1])
print(a1.dtype)  # torch.float32

b1 = torch.tensor([1])
print(b1.dtype)  # torch.int64

a2 = torch.Tensor([1.0])
print(a2.dtype)  # torch.float32

b2 = torch.tensor([1.0])
print(b2.dtype)  # torch.float32
